//
//  NetMoveCar.swift
//  carcareIOS
//
//  Created by ileo on 16/9/21.
//  Copyright © 2016年 chezheng. All rights reserved.
//

import UIKit

class NetMoveCar: Net {
    
    
    override init() {
        super.init()
        self.config(withNetRequest: NetEngineServer.iczConfig())
    }
    
    //获取勿扰消息
    class func getNoBotherInfo(success: @escaping ((MoveCarSettingModel) -> Void), fail: @escaping (() -> Void)){
        NetMoveCar().config(withPath: "/moveCar/querySetting", params: nil, type: .POST).request(success: { info in
            success(MoveCarSettingModel(dictionary: info?["data"] as! [AnyHashable : Any]!))
            }, failure: {info in
                fail()
        })
    }
    
    //设置勿扰消息
    class func setNoBotherInfo(open: Bool, start: String, end: String, success: @escaping (() -> Void), fail: @escaping (() -> Void)) {
        let load:Int = Int(RequestLoad.showErrorTips.rawValue) | Int(RequestLoad.showLoading.rawValue) | Int(RequestLoad.showSuccessTips.rawValue)
        NetMoveCar().config(withPath: "/moveCar/setting", params: ["flag":(open ? 1 : 0), "startTime": start, "endTime": end], type: .POST).config(withLoadShow: RequestLoad(rawValue: load)!).request(success: { info in
            success()
            }, failure: {info in
                fail()
        })
    }
    
    //随机获取个性QR
    class func getRandomQRTypeInfo(currentTypeID: Int, success: @escaping ((MoveCarTypeQRModel) -> Void), fail: @escaping (() -> Void)){
        let load:Int = Int(RequestLoad.showErrorTips.rawValue)
        NetMoveCar().config(withPath: "/moveCar/changeTdCodeStyle", params: ["styleId": currentTypeID], type: .POST).config(withLoadShow:RequestLoad(rawValue: load)!).request(success: { info in
            success(MoveCarTypeQRModel(dictionary: info?["data"] as! [AnyHashable : Any]!))
            }, failure: {info in
                fail()
        })
    }
    
    //获取用户QR列表
    class func getQRList(success: @escaping (([MoveCarQRModel]) -> Void), fail: @escaping (() -> Void)){
        NetMoveCar().config(withPath: "/moveCar/getTdCodeList", params: nil, type: .POST).request(success: { info in
            
            let models:[MoveCarQRModel] = ValuePropertyObject.instanceObjects(with: (info?["data"] as! [AnyHashable : Any])["tdCodeList"] as! [[AnyHashable : Any]], createObject: { () -> ValuePropertyObject? in
                return MoveCarQRModel()
            }) as! [MoveCarQRModel];
            let styles:[AnyHashable : Any] = (info?["data"] as! [AnyHashable : Any])["styleMap"] as! [AnyHashable : Any]
            
            let semap:DispatchSemaphore = DispatchSemaphore(value: 0)
            for model in models {
                let key:String = ((model.styleId != nil) ? "\(model.styleId!.intValue)" : "0")
                let dic: [AnyHashable : Any] = styles[key] as! [AnyHashable : Any]!
                let typeModel: MoveCarTypeQRModel = MoveCarTypeQRModel(dictionary: dic)
                MoveCarManager.qrType(qrURL: model.tdCodeUrl, qrTypeModel: typeModel, success: { (new) in
                    model.styleModel = new
                    semap.signal()
                    }, fail: {
                        model.styleModel = typeModel
                        semap.signal()
                })
                let _ = semap.wait(timeout: DispatchTime.distantFuture)
            }
            
            success(models)
            }, failure: {info in
                fail()
        })
    }
    
    //编辑QR
    class func updateQR(model: MoveCarQRModel, success: @escaping (() -> Void), fail: @escaping (() -> Void)) {
        let load:Int = Int(RequestLoad.showErrorTips.rawValue) | Int(RequestLoad.showLoading.rawValue)
        NetMoveCar().config(withPath: "/moveCar/editTdCode", params: model.propertyDictionaryValue(), type: .POST).config(withLoadShow: RequestLoad(rawValue: load)!).request(success: { info in
            success()
            }, failure: {info in
                fail()
        })
    }
    
    //删除QR
    class func delQR(qrID: Int, success: @escaping (() -> Void), fail: @escaping (() -> Void)) {
        let load:Int = Int(RequestLoad.showErrorTips.rawValue) | Int(RequestLoad.showLoading.rawValue)
        NetMoveCar().config(withPath: "/moveCar/delTdCode", params: ["id": qrID], type: .POST).config(withLoadShow: RequestLoad(rawValue: load)!).request(success: { info in
            success()
            }, failure: {info in
                fail()
        })
    }
    
    //获取URL
    class func getQRURL(phone: String, carno:String, success: @escaping ((String) -> Void), fail: @escaping (() -> Void)) {
        let load:Int = Int(RequestLoad.showErrorTips.rawValue) | Int(RequestLoad.showLoading.rawValue)
        NetMoveCar().config(withPath: "/moveCar/jumpUrl", params: ["phone": phone, "carno": carno], type: .POST).config(withLoadShow: RequestLoad(rawValue: load)!).request(success: { info in
            let data:[AnyHashable : Any] = info?["data"] as! [AnyHashable : Any]
            let url: String = data["url"] as! String
            success(url)
            }, failure: {info in
                fail()
        })
    }
    
    override func requestCommonParams() -> [AnyHashable : Any] {
        return ["userid": AIUserInfo.sharedInstance().userID, "session": AIUserInfo.sharedInstance().session]
    }
}
